<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- v-auth="'bus:ht:m8ViewConstract:export'" -->
        <a-button type="default" @click="handleExport()" v-auth="'bus:ht:m8ViewConstract:export'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBusHtM8ViewConstractList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { m8ViewConstractDelete, m8ViewConstractListData } from '/@/api/bus/ht/m8ViewConstract';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { downloadByUrl, downloadByUrl2 } from '/@/utils/file/download';
  import { useGlobSetting } from '/@/hooks/setting';
  import { areaTreeData } from '/@/api/sys/desktop';

  const { t } = useI18n('bus.ht.m8ViewConstract');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('医废合同管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('医疗机构'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('合同状态'),
        field: 'htStatus',
        component: 'Select',
        componentProps: {
          dictType: 'yf_htzt_status',
          allowClear: true,
        },
      },
      {
        label: t('最新合同'),
        field: 'bnew',
        component: 'CheckboxGroup',
        componentProps: {
          options: [{ label: '', value: '1' }],
        },
      },
      {
        label: t('区域'),
        field: 'areaCode',
        // fieldLabel: 'm8ViewHosp.areaName',
        component: 'TreeSelect',
        componentProps: {
          api: areaTreeData,
          canSelectParent: false,
          allowClear: true,
        },
        itemProps: { validateTrigger: 'blur' },
      },
      {
        label: t('医疗机构性质'),
        field: 'ctype',
        component: 'Input',
        componentProps: {
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('医疗机构'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('地址'),
      dataIndex: 'addr',
      key: 'a.addr',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('区域'),
      dataIndex: 'area',
      key: 'a.area',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('医疗机构性质'),
      dataIndex: 'ctype',
      key: 'a.ctype',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('生效日期'),
      dataIndex: 'validData',
      key: 'a.valid_data',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('终止日期'),
      dataIndex: 'stopData',
      key: 'a.stop_data',
      sorter: true,
      format: 'yyyy-MM-dd',
      width: 120,
      align: 'center',
    },
    {
      title: t('将要到期天数'),
      dataIndex: 'syDays',
      key: 'a.sy_days',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('合同状态'),
      dataIndex: 'htStatus',
      key: 'a.ht_status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'yf_htzt_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑医废合同'),
        onClick: handleForm.bind(this, { name: record.name }),
        auth: 'bus:ht:m8ViewConstract:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除医废合同'),
        popConfirm: {
          title: t('是否确认删除医废合同'),
          confirm: handleDelete.bind(this, { name: record.name }),
        },
        auth: 'bus:ht:m8ViewConstract:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: m8ViewConstractListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    ellipsis: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    // downloadByUrl({
    //   url: ctxAdminPath + '/bus/ht/m8ViewConstract/exportData',
    //   target: '_self',
    // });
    downloadByUrl2({
      url: ctxAdminPath + '/bus/ht/m8ViewConstract/exportData',
      params: {},
      data: getForm().getFieldsValue(),
    });
  }

  async function handleDelete(record: Recordable) {
    const res = await m8ViewConstractDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
